package com.ruoyi.contactSys.controller;

import java.util.Date;
import java.util.List;
import javax.servlet.http.HttpServletResponse;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.PutMapping;
import org.springframework.web.bind.annotation.DeleteMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import com.ruoyi.common.annotation.Log;
import com.ruoyi.common.core.controller.BaseController;
import com.ruoyi.common.core.domain.AjaxResult;
import com.ruoyi.common.enums.BusinessType;
import com.ruoyi.contactSys.domain.TbFeedback;
import com.ruoyi.contactSys.service.ITbFeedbackService;
import com.ruoyi.common.utils.poi.ExcelUtil;
import com.ruoyi.common.core.page.TableDataInfo;

/**
 * 公众反馈Controller
 *
 * @author ruoyi
 * @date 2025-11-06
 */
@RestController
@RequestMapping("/contactSys/feedback")
public class TbFeedbackController extends BaseController
{
    @Autowired
    private ITbFeedbackService tbFeedbackService;

    // ---------------------------------------
    // 游客接口（无需登录）
    // ---------------------------------------

    /** 游客新增反馈 */
    @Log(title = "公众反馈", businessType = BusinessType.INSERT)
    @PostMapping("/public/add")
    public AjaxResult guestAdd(@RequestBody TbFeedback tbFeedback)
    {
        tbFeedback.setSubmitTime(new Date()); // 当前时间
        tbFeedback.setStatus("未处理");        // 默认状态
        tbFeedback.setIsDeleted(0);          // 默认未删除
        return toAjax(tbFeedbackService.insertTbFeedback(tbFeedback));
    }

    /** 游客查看反馈列表（未删除的） */
    @GetMapping("/public/list")
    public AjaxResult guestList()
    {
        TbFeedback filter = new TbFeedback();
        filter.setIsDeleted(0); // 只显示未删除
        List<TbFeedback> list = tbFeedbackService.selectTbFeedbackList(filter);
        return AjaxResult.success(list);
    }

    /** 游客修改自己的反馈 */
    @PutMapping("/public/update")
    public AjaxResult guestUpdate(@RequestBody TbFeedback tbFeedback)
    {
        // 仅允许游客可修改字段
        TbFeedback update = new TbFeedback();
        update.setId(tbFeedback.getId());
        update.setName(tbFeedback.getName());
        update.setEmail(tbFeedback.getEmail());
        update.setPhone(tbFeedback.getPhone());
        update.setOrganization(tbFeedback.getOrganization());
        update.setFeedbackType(tbFeedback.getFeedbackType()); // 修正拼写
        update.setContent(tbFeedback.getContent());
        update.setSubmitTime(new Date()); // 更新提交时间

        return toAjax(tbFeedbackService.updateTbFeedback(update));
    }

    /** 游客删除自己的反馈 */
    @DeleteMapping("/public/delete/{id}")
    public AjaxResult guestDelete(@PathVariable Long id)
    {
        // 逻辑删除
        return toAjax(tbFeedbackService.deleteTbFeedbackByIds(new Long[]{id}));
    }

    /**
     * 查询公众反馈列表
     */
    @PreAuthorize("@ss.hasPermi('contactSys:feedback:list')")
    @GetMapping("/list")
    public TableDataInfo list(TbFeedback tbFeedback)
    {
        startPage();
        List<TbFeedback> list = tbFeedbackService.selectTbFeedbackList(tbFeedback);
        return getDataTable(list);
    }

    /**
     * 导出公众反馈列表
     */
    @PreAuthorize("@ss.hasPermi('contactSys:feedback:export')")
    @Log(title = "公众反馈", businessType = BusinessType.EXPORT)
    @PostMapping("/export")
    public void export(HttpServletResponse response, TbFeedback tbFeedback)
    {
        List<TbFeedback> list = tbFeedbackService.selectTbFeedbackList(tbFeedback);
        ExcelUtil<TbFeedback> util = new ExcelUtil<TbFeedback>(TbFeedback.class);
        util.exportExcel(response, list, "公众反馈数据");
    }

    /**
     * 获取公众反馈详细信息
     */
    @PreAuthorize("@ss.hasPermi('contactSys:feedback:query')")
    @GetMapping(value = "/{id}")
    public AjaxResult getInfo(@PathVariable("id") Long id)
    {
        return success(tbFeedbackService.selectTbFeedbackById(id));
    }

    /**
     * 新增公众反馈
     */
    @PreAuthorize("@ss.hasPermi('contactSys:feedback:add')")
    @Log(title = "公众反馈", businessType = BusinessType.INSERT)
    @PostMapping
    public AjaxResult add(@RequestBody TbFeedback tbFeedback)
    {
        return toAjax(tbFeedbackService.insertTbFeedback(tbFeedback));
    }

    /**
     * 修改公众反馈
     */
    @PreAuthorize("@ss.hasPermi('contactSys:feedback:edit')")
    @Log(title = "公众反馈", businessType = BusinessType.UPDATE)
    @PutMapping
    public AjaxResult edit(@RequestBody TbFeedback tbFeedback)
    {
        return toAjax(tbFeedbackService.updateTbFeedback(tbFeedback));
    }

    /**
     * 删除公众反馈
     */
    @PreAuthorize("@ss.hasPermi('contactSys:feedback:remove')")
    @Log(title = "公众反馈", businessType = BusinessType.DELETE)
	@DeleteMapping("/{ids}")
    public AjaxResult remove(@PathVariable Long[] ids)
    {
        return toAjax(tbFeedbackService.deleteTbFeedbackByIds(ids));
    }
}
